package BookStore.managers_impl.DeliveryBookStoreMgr;


import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;

import BookStore.managers.DeliveryBookStoreMgr_Iface.DeliveryBookStoreMgrPOA;
import OnlineLibrary.NotificationsBuyingMgr_Iface.NotificationBuyingMgr;
import OnlineLibrary.NotificationsBuyingMgr_Iface.NotificationBuyingMgrHelper;

/**
 * 
 * Manager of book returns
 */
public class DeliveryBookStoreMgr extends DeliveryBookStoreMgrPOA{
	
	
	@Override
	public void returnBook(int sellingNumber) {
		// Register the return? -> No stock so no need
		// Only a notification to the OnlineLibrary
		System.out.println("return book :"+sellingNumber);
		NotificationBuyingMgr library = getNotifMgr();
		library.notifCreditNote(sellingNumber);
		
	}
	
	public NotificationBuyingMgr getNotifMgr(){
		// get the root naming context
	      org.omg.CORBA.Object objRef = null;
		try {
			objRef = _orb().resolve_initial_references("NameService");
		} catch (InvalidName e1) {
			System.out.println("Can't get the NamingService reference");
			e1.printStackTrace();
		}
		NamingContext context = NamingContextHelper.narrow(objRef);

		NameComponent[] name = new NameComponent[] { new NameComponent(
				"Library", "Object" ) };
		try {
			return NotificationBuyingMgrHelper.narrow(context.resolve(name));
		} catch (Exception e) {
			return null;
		}

	}

}
